package com.jingdianjichi.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.application.controller.convert.AuthPermissionDTOConvert;
import com.jingdianjichi.application.controller.dto.AuthPermissionDTO;

import com.jingdianjichi.auth.domain.bo.AuthPermissionBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Permission;

/**
 * ClassName:AuthPermissionController
 * Package:com.jingdianjichi.application.controller.dto
 * Description:
 *
 * @Author fst
 * @Create 2024/10/10 16:33
 * @Version 1.0
 */

@RequestMapping("/permission/")
@Slf4j
@RestController
public class AuthPermissionController
{
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getName(),"name不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getPermissionKey(),"permissionKey不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANDE.convertDTOTOBOPermission(authPermissionDTO);

           authPermissionDomainService.add(authPermissionBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthPermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getName(),"name不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getPermissionKey(),"permissionKey不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getId(),"id不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"parentId不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANDE.convertDTOTOBOPermission(authPermissionDTO);

            authPermissionDomainService.update(authPermissionBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthPermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }

            Preconditions.checkNotNull(authPermissionDTO.getId(), "id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANDE.convertDTOTOBOPermission(authPermissionDTO);

            authPermissionDomainService.delete(authPermissionBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthPermissionController.delete.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping("getPermission")
    public Result<Permission>getPermission(String userName) {
        try {
            log.info("PermissionController.getPermission.userName{}", userName);

            Preconditions.checkNotNull(userName, "userName不能为空");

            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}
